<template>
  <div class="news-carousel" @mouseenter="pauseAutoPlay" @mouseleave="startAutoPlay">
    <div class="carousel-wrapper">
      <div
          v-for="(news, index) in newsItems"
          :key="news.id"
          class="carousel-slide"
          :class="{ active: index === activeIndex }">
        <img :src="news.imagUrl" :alt="news.newsTitle" class="news-image" />
        <div class="carousel-caption">
          <h3>{{ news.newsTitle }}</h3>
          <br>
          <span style="margin-left: 50%">{{ news.newsSource }}</span>
        </div>
      </div>
    </div>
    <div class="carousel-dots">
      <span
          v-for="(news, index) in newsItems"
          :key="news.id"
          :class="{ active: index === activeIndex }"
          @click="goToSlide(index)"
      ></span>
    </div>
    <button @click="prevSlide" class="prev-button">❮</button>
    <button @click="nextSlide" class="next-button">❯</button>
  </div>
</template>

<script>
export default {
  name: 'NewsCarousel',
  props: {
    newsItems: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      activeIndex: 0,
      interval: null,
    };
  },
  mounted() {
    this.startAutoPlay();
  },
  methods: {
    startAutoPlay() {
      this.interval = setInterval(this.nextSlide, 3000);
    },
    pauseAutoPlay() {
      clearInterval(this.interval);
    },
    nextSlide() {
      this.activeIndex = (this.activeIndex + 1) % this.newsItems.length;
    },
    prevSlide() {
      this.activeIndex =
          (this.activeIndex - 1 + this.newsItems.length) % this.newsItems.length;
    },
    goToSlide(index) {
      this.activeIndex = index;
    },
  },
  beforeDestroy() {
    this.pauseAutoPlay();
  },
};
</script>

<style scoped>
.news-carousel {
  position: relative;
  width: 360px;
  height: 302px;
  overflow: hidden;
  border-radius: 10px;
  background: linear-gradient(135deg, #ff7e5f, #feb47b);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
}

.carousel-wrapper {
  position: relative;
  width: 100%;
  height: 100%;
}

.carousel-slide {
  position: absolute;
  width: 100%;
  height: 70%;
  opacity: 0;
  transition: opacity 0.5s ease, transform 0.5s ease;
  transform: translateX(100%);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
}

.carousel-slide.active {
  opacity: 1;
  transform: translateX(0);
}

.news-image {
  width: 100%;
  height: 70%;
  object-fit: cover;
  border-radius: 10px 10px 0 0;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease-in-out;
}

.carousel-caption {
  background-color: rgba(0, 0, 0, 0.7);
  color: white;
  font-size: 12px;
  width: 100%;
  height: 30%;
  text-align: center;
  line-height: 8px;
}
.details-link {
  color: #ffcc00;
  text-decoration: underline;
  font-size: 12px;
}

.carousel-dots {
  position: absolute;
  bottom: 5px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 5px;
}

.carousel-dots span {
  width: 10px;
  height: 10px;
  background-color: rgba(255, 255, 255, 0.5);
  border-radius: 50%;
  cursor: pointer;
  transition: background-color 0.3s ease, transform 0.3s ease;
}

.carousel-dots span.active {
  background-color: rgba(255, 255, 255, 1);
  transform: scale(1.2);
}

button {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  border: none;
  padding: 8px 12px;
  cursor: pointer;
  transition: background-color 0.3s ease;
  border-radius: 50%;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

button:hover {
  background-color: rgba(0, 0, 0, 0.8);
}

.prev-button {
  left: 10px;
}

.next-button {
  right: 10px;
}
</style>
